package com.wenhao.leetcode.easy;

/**
 * 题目：https://leetcode-cn.com/problems/length-of-last-word/
 * 最后一个单词的长度
 *
 * 注意要处理最后有多余空格的情况
 *
 * @author Wenhao Tong
 * @Description
 * @create 2021-07-08 23:55
 */
public class LeetCode58 {
    public int lengthOfLastWord(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        int length = 0;
        boolean flag = false;
        for (int i = s.length() - 1;i > -1;i--) {
            if (s.charAt(i) == ' ' && flag) {
                return length;
            }
            if (s.charAt(i) != ' ') {
                flag = true;
                length++;
            }
        }
        return length;
    }
}
